<?php
// 1908080121李定邦、1902030115李龙烽
namespace app\admin\controller;
use app\BaseController;
use think\App;

class ResRec extends BaseController
{
    private $_res;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->checkIsAdminLogin();
        $this->_res = app('db')->name('res');
    }

    /**
     * 回复列表
     * @return mixed
     */
    public function index() {
        $keyword = $this->request->get('keyword');
        $query   = $this->_res->where("r_content", "LIKE", "%$keyword%")->where("is_del", 1);
        $data    = $query->paginate(10);
        $page    = $data->render();
        $data = [
            'list'     => $data,
            'page'     => $page,
        ];

        return app('view')->fetch('', $data);
    }

    /**
     * 添加
     * @return void
     */
    public function add() {
        if ($this->request->isPost()) {
            $r_content  = $this->request->post('r_content', '', 'trim');
            $r_unick    = app('db')->name('admin_user')->where('id', session('login_admin_id'))->value('username');
            $r_mid      = $this->request->post('r_mid', '', 'trim');
            $data = [
                'r_content' => $r_content,
                'r_unick'   => $r_unick,
                'r_mid'     => $r_mid,
                'is_del'    => 1,
            ];
            $res = app('db')->name('res')->insert($data);
            $res ? $this->success('添加成功', '/res/rec') : $this->error('添加失败');
        } else {
            $forums = app('db')->name('mes')->where('is_del', 0)->select();
            return app('view')->fetch('', ['forum_list' => $forums]);
        }
    }

    /**
     *
     * @return \think\response\Json
     */
    public function delete() {
        $rid = $this->request->get('rid');
        $rid = is_array($rid) ? $rid : [$rid];
        $res = $this->_res->whereIn('r_id', $rid)->delete();
        $data = [
            'rs' => true,
            'msg' => $res ? '彻底删除成功' : '未知错误'
        ];

        return json($data);
    }

    /**
     * 回复板块
     * @return \think\response\Json
     */
    public function normal() {
        $rid = $this->request->get('rid');
        $rid = is_array($rid) ? $rid : [$rid];
        $res = $this->_res->whereIn('r_id', $rid)->update(['is_del' => 0]);
        $data = [
            'rs' => true,
            'msg' => $res ? '恢复成功' : '未知错误'
        ];

        return json($data);
    }

}